#include "ebutton.h"
#include <QPainter>

EButton::EButton(QWidget *parent) : QPushButton(parent)
{
    setDown(false);
    setFocusPolicy(Qt::NoFocus);
    setFlat(true);
}

EButton::~EButton()
{

}

void EButton::SetTexts(const char *text)
{
    m_text = text;
}

void EButton::SetTexts(const QString &text)
{
    m_text = text;
}

void EButton::SetButtonIcons(const QIcon &normal, const QIcon &focus, const QIcon &pressed)
{
    m_normalIcon = normal;
    m_focusIcon = focus;
    m_pressedIcon = pressed;
    setIcon(m_normalIcon);
}

void EButton::SetButtonIcons(const char *path)
{
    QString p = ":/res/button/";
    p += path;

    m_normalIcon = QIcon(p + "/normal.png");
    m_focusIcon = QIcon(p + "/focus.png");
    m_pressedIcon = QIcon(p + "/pressed.png");

    resize(78, 78);
    setIcon(m_normalIcon);
    setIconSize(QSize(78, 78));
}

void EButton::enterEvent(QEvent *event)
{
    if( isEnabled() && !m_focusIcon.isNull())
        setIcon(m_focusIcon);
}

void EButton::leaveEvent(QEvent *event)
{
    if( isEnabled() )
        setIcon(m_normalIcon);
}

void EButton::mousePressEvent(QMouseEvent *event)
{
    if( isEnabled() && !m_pressedIcon.isNull() )
        setIcon(m_pressedIcon);
    QPushButton::mousePressEvent(event);
}

void EButton::mouseReleaseEvent(QMouseEvent *event)
{
    if( isEnabled() && m_focusIcon.isNull() )
        setIcon(m_focusIcon);
    QPushButton::mouseReleaseEvent(event);
}

void EButton::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);
    QPainter lPainter(this);
    if (m_text != "")
        lPainter.drawText(rect(),Qt::AlignCenter, m_text);
}
